<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang="zh-CN">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="本地通知">
<meta property="og:type" content="article">
<meta property="og:title" content="本地通知">
<meta property="og:url" content="http://yoursite.com/2016/10/24/本地通知/index.html">
<meta property="og:site_name" content="Erhqe">
<meta property="og:description" content="本地通知">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="https://ws2.sinaimg.cn/large/006tNbRwly1fwjbrego6oj30fw066n0l.jpg">
<meta property="og:image" content="https://ws3.sinaimg.cn/large/006tNbRwly1fwjd9ec7kxj30eu05pq3f.jpg">
<meta property="og:image" content="https://ws3.sinaimg.cn/large/006tNbRwly1fwjde2l9oij30fw0mmjt5.jpg">
<meta property="og:image" content="https://ws4.sinaimg.cn/large/006tNbRwly1fwjdz68s9lj314w0ta0xl.jpg">
<meta property="og:image" content="https://ws2.sinaimg.cn/large/006tNbRwly1fwje00e5uqj30du04a0tc.jpg">
<meta property="og:image" content="https://ws4.sinaimg.cn/large/006tNbRwly1fwjftrykf8j316w0fywj1.jpg">
<meta property="og:image" content="https://ws4.sinaimg.cn/large/006tNbRwly1fwjfuwnp7tj31jw0iw43t.jpg">
<meta property="og:updated_time" content="2019-07-19T05:40:06.074Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="本地通知">
<meta name="twitter:description" content="本地通知">
<meta name="twitter:image" content="https://ws2.sinaimg.cn/large/006tNbRwly1fwjbrego6oj30fw066n0l.jpg">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2016/10/24/本地通知/"/>





  <title>本地通知 | Erhqe</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Erhqe</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            Categories
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            Archives
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2016/10/24/本地通知/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Erhqe">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/10.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Erhqe">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">本地通知</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2016-10-24T14:40:21+08:00">
                2016-10-24
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">In</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/本地通知/" itemprop="url" rel="index">
                    <span itemprop="name">本地通知</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2016/10/24/本地通知/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/2016/10/24/本地通知/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h3 id="本地通知"><a href="#本地通知" class="headerlink" title="本地通知"></a>本地通知<a id="more"></a></h3><h4 id="UNMutableNotificationContent-内容"><a href="#UNMutableNotificationContent-内容" class="headerlink" title="UNMutableNotificationContent-内容"></a>UNMutableNotificationContent-内容</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">fileprivate lazy var content : UNMutableNotificationContent = &#123;</span><br><span class="line">let content = UNMutableNotificationContent()</span><br><span class="line">content.title = &quot;ios10通知标题&quot;</span><br><span class="line">content.subtitle = &quot;副标题&quot;</span><br><span class="line">content.body = &quot;内容,内容文字,内容文字,内容文字,内容文字,内容文字,内容文字,内容文字&quot;</span><br><span class="line">content.badge = 1</span><br><span class="line">return content</span><br><span class="line">&#125;()</span><br></pre></td></tr></table></figure>
<p><img src="https://ws2.sinaimg.cn/large/006tNbRwly1fwjbrego6oj30fw066n0l.jpg" alt=""></p>
<h4 id="UNNotificationSound-声音"><a href="#UNNotificationSound-声音" class="headerlink" title="UNNotificationSound - 声音"></a>UNNotificationSound - 声音</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">// 通知声音</span><br><span class="line">let sounnd = UNNotificationSound.default()</span><br><span class="line">content.sound = sounnd</span><br></pre></td></tr></table></figure>
<h4 id="UNPushNotificaitonTrigger-触发器"><a href="#UNPushNotificaitonTrigger-触发器" class="headerlink" title="UNPushNotificaitonTrigger - 触发器"></a>UNPushNotificaitonTrigger - 触发器</h4><p><strong>远程推送由系统创建</strong></p>
<h4 id="UNTimeIntervalNotificaitonTrigger-时间触发器"><a href="#UNTimeIntervalNotificaitonTrigger-时间触发器" class="headerlink" title="UNTimeIntervalNotificaitonTrigger - 时间触发器"></a>UNTimeIntervalNotificaitonTrigger - 时间触发器</h4><figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> trigger = <span class="type">UNTimeIntervalNotificationTrigger</span>(timeInterval: <span class="number">5</span>, repeats: <span class="literal">false</span>)</span><br></pre></td></tr></table></figure>
<h4 id="UNCalendarNotificaitonTrigger-日期触发器"><a href="#UNCalendarNotificaitonTrigger-日期触发器" class="headerlink" title="UNCalendarNotificaitonTrigger - 日期触发器"></a>UNCalendarNotificaitonTrigger - 日期触发器</h4><figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//每天12点</span></span><br><span class="line"><span class="keyword">var</span> components = <span class="type">DateComponents</span>()</span><br><span class="line">components.hour = <span class="number">12</span></span><br><span class="line">components.minute = <span class="number">0</span></span><br><span class="line"><span class="keyword">let</span> calenderTrigger = <span class="type">UNCalendarNotificationTrigger</span>(dateMatching: components, repeats: <span class="literal">false</span>)</span><br></pre></td></tr></table></figure>
<h4 id="UNLocationNotificaitonTrigger-位置触发器"><a href="#UNLocationNotificaitonTrigger-位置触发器" class="headerlink" title="UNLocationNotificaitonTrigger - 位置触发器"></a>UNLocationNotificaitonTrigger - 位置触发器</h4><figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> center = <span class="type">CLLocationCoordinate2DMake</span>(<span class="number">111.65</span>, <span class="number">40.82</span>)  <span class="comment">//位置</span></span><br><span class="line"><span class="keyword">let</span> region = <span class="type">CLCircularRegion</span>(center: center, radius: <span class="number">2000.0</span>, identifier: <span class="string">"Home"</span>)   <span class="comment">//地区</span></span><br><span class="line">region.notifyOnEntry = <span class="literal">true</span>     <span class="comment">//进来</span></span><br><span class="line">region.notifyOnExit = <span class="literal">false</span>     <span class="comment">//出去</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> locationTrigger = <span class="type">UNLocationNotificationTrigger</span>(region: region, repeats: <span class="literal">false</span>)</span><br></pre></td></tr></table></figure>
<h4 id="UNNotificationRequest-通知请求"><a href="#UNNotificationRequest-通知请求" class="headerlink" title="UNNotificationRequest - 通知请求"></a>UNNotificationRequest - 通知请求</h4><figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> request = <span class="type">UNNotificationRequest</span>(identifier: requestID, content: content, trigger: locationTrigger)</span><br><span class="line"><span class="comment">//	添加通知</span></span><br><span class="line"><span class="type">UNUserNotificationCenter</span>.current().add(request) &#123; (error) <span class="keyword">in</span></span><br><span class="line"><span class="keyword">guard</span> <span class="keyword">let</span> error1 = error <span class="keyword">else</span> &#123; <span class="keyword">return</span> &#125;</span><br><span class="line"><span class="built_in">print</span>(error1)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="UNNotificationationAttachment-附件"><a href="#UNNotificationationAttachment-附件" class="headerlink" title="UNNotificationationAttachment - 附件"></a>UNNotificationationAttachment - 附件</h4><p><img src="https://ws3.sinaimg.cn/large/006tNbRwly1fwjd9ec7kxj30eu05pq3f.jpg" alt=""></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">let imageURL = Bundle.main.url(forResource: &quot;123&quot;, withExtension: &quot;png&quot;)</span><br><span class="line">guard let url = imageURL else &#123; return &#125;</span><br><span class="line">let attachment = try? UNNotificationAttachment(identifier: &quot;ImageID&quot;, url:url, options: nil)</span><br><span class="line">guard let arrach = attachment else &#123; return &#125;</span><br><span class="line"></span><br><span class="line">content.attachments = [arrach]  //强然是数组 但是添加多个只能显示第一个</span><br></pre></td></tr></table></figure>
<p><img src="https://ws3.sinaimg.cn/large/006tNbRwly1fwjde2l9oij30fw0mmjt5.jpg" alt=""></p>
<p><strong>时间触发器例子</strong></p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">fileprivate</span> <span class="built_in">lazy</span> <span class="keyword">var</span> content : <span class="type">UNMutableNotificationContent</span> = &#123;</span><br><span class="line"><span class="keyword">let</span> content = <span class="type">UNMutableNotificationContent</span>()</span><br><span class="line">content.title = <span class="string">"ios10通知标题"</span></span><br><span class="line">content.subtitle = <span class="string">"副标题"</span></span><br><span class="line">content.body = <span class="string">"内容,内容文字,内容文字,内容文字,内容文字,内容文字,内容文字,内容文字"</span></span><br><span class="line">content.badge = <span class="number">1</span></span><br><span class="line"><span class="keyword">return</span> content</span><br><span class="line"></span><br><span class="line">&#125;()</span><br><span class="line"></span><br><span class="line"><span class="comment">// MARK: - 秒</span></span><br><span class="line"><span class="class"><span class="keyword">extension</span> <span class="title">ViewController</span> </span>&#123;</span><br><span class="line"><span class="meta">@IBAction</span> <span class="function"><span class="keyword">func</span> <span class="title">normalNtification</span><span class="params">(<span class="number">_</span> sender: UIButton)</span></span> &#123;</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> sounnd = <span class="type">UNNotificationSound</span>.<span class="keyword">default</span>()</span><br><span class="line">content.sound = sounnd</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> trigger = <span class="type">UNTimeIntervalNotificationTrigger</span>(timeInterval: <span class="number">5</span>, repeats: <span class="literal">false</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> requestID = <span class="string">"requestID"</span></span><br><span class="line"><span class="keyword">let</span> request = <span class="type">UNNotificationRequest</span>(identifier: requestID, content: content, trigger: trigger)</span><br><span class="line"></span><br><span class="line"><span class="type">UNUserNotificationCenter</span>.current().add(request) &#123; (error) <span class="keyword">in</span></span><br><span class="line"><span class="keyword">guard</span> <span class="keyword">let</span> error1 = error <span class="keyword">else</span> &#123; <span class="keyword">return</span> &#125;</span><br><span class="line"><span class="built_in">print</span>(error1)</span><br><span class="line">&#125;</span><br><span class="line">&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>位置触发器例子</strong></p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// MARK: - 地理</span></span><br><span class="line"><span class="class"><span class="keyword">extension</span> <span class="title">ViewController</span> </span>&#123;</span><br><span class="line"><span class="meta">@IBAction</span> <span class="function"><span class="keyword">func</span> <span class="title">locationNotifi</span><span class="params">(<span class="number">_</span> sender: UIButton)</span></span> &#123;</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> sounnd = <span class="type">UNNotificationSound</span>.<span class="keyword">default</span>()</span><br><span class="line">content.sound = sounnd</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> center = <span class="type">CLLocationCoordinate2DMake</span>(<span class="number">111.65</span>, <span class="number">40.82</span>)  <span class="comment">//位置</span></span><br><span class="line"><span class="keyword">let</span> region = <span class="type">CLCircularRegion</span>(center: center, radius: <span class="number">2000.0</span>, identifier: <span class="string">"Home"</span>)   <span class="comment">//地区</span></span><br><span class="line">region.notifyOnEntry = <span class="literal">true</span>     <span class="comment">//进来</span></span><br><span class="line">region.notifyOnExit = <span class="literal">false</span>     <span class="comment">//出去</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> locationTrigger = <span class="type">UNLocationNotificationTrigger</span>(region: region, repeats: <span class="literal">false</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> requestID = <span class="string">"locationTriggerRequestID"</span></span><br><span class="line"><span class="keyword">let</span> request = <span class="type">UNNotificationRequest</span>(identifier: requestID, content: content, trigger: locationTrigger)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="type">UNUserNotificationCenter</span>.current().add(request) &#123; (error) <span class="keyword">in</span></span><br><span class="line"><span class="keyword">guard</span> <span class="keyword">let</span> error1 = error <span class="keyword">else</span> &#123; <span class="keyword">return</span> &#125;</span><br><span class="line"><span class="built_in">print</span>(error1)</span><br><span class="line">&#125;</span><br><span class="line">&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>日历触发器例子</strong></p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// MARK: - 日历通知</span></span><br><span class="line"><span class="class"><span class="keyword">extension</span> <span class="title">ViewController</span> </span>&#123;</span><br><span class="line"><span class="meta">@IBAction</span> <span class="function"><span class="keyword">func</span> <span class="title">calenderNotifi</span><span class="params">(<span class="number">_</span> sender: UIButton)</span></span> &#123;</span><br><span class="line"><span class="comment">//每天12点</span></span><br><span class="line"><span class="keyword">var</span> components = <span class="type">DateComponents</span>()</span><br><span class="line">components.hour = <span class="number">12</span></span><br><span class="line">components.minute = <span class="number">0</span></span><br><span class="line"><span class="keyword">let</span> calenderTrigger = <span class="type">UNCalendarNotificationTrigger</span>(dateMatching: components, repeats: <span class="literal">false</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> sounnd = <span class="type">UNNotificationSound</span>.<span class="keyword">default</span>()</span><br><span class="line">content.sound = sounnd</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> requestID = <span class="string">"requestID"</span></span><br><span class="line"><span class="keyword">let</span> request = <span class="type">UNNotificationRequest</span>(identifier: requestID, content: content, trigger: calenderTrigger)</span><br><span class="line"></span><br><span class="line"><span class="type">UNUserNotificationCenter</span>.current().add(request) &#123; (error) <span class="keyword">in</span></span><br><span class="line"><span class="keyword">guard</span> <span class="keyword">let</span> error1 = error <span class="keyword">else</span> &#123; <span class="keyword">return</span> &#125;</span><br><span class="line"><span class="built_in">print</span>(error1)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//DateComponents 的注意点 ,有了时间并不能得到Weekday</span></span><br><span class="line"><span class="keyword">var</span> components1 = <span class="type">DateComponents</span>()</span><br><span class="line">components1.year = <span class="number">2018</span></span><br><span class="line">components1.month = <span class="number">10</span></span><br><span class="line">components1.day = <span class="number">26</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//根据calender 计算是周几</span></span><br><span class="line"><span class="keyword">let</span> caleder = <span class="type">Calendar</span>(identifier: .gregorian)</span><br><span class="line"><span class="keyword">let</span> date = caleder.date(from: components1)</span><br><span class="line"><span class="keyword">let</span> weekday = caleder.component(<span class="type">Calendar</span>.<span class="type">Component</span>.weekday, from: date!)</span><br><span class="line"><span class="built_in">print</span>(weekday)</span><br><span class="line">components1.weekday = weekday</span><br><span class="line">&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>交互Action</strong></p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// MARK: - ActionNotification</span></span><br><span class="line"><span class="class"><span class="keyword">extension</span> <span class="title">ViewController</span> </span>&#123;</span><br><span class="line"><span class="meta">@IBAction</span> <span class="function"><span class="keyword">func</span> <span class="title">actionNotifi</span><span class="params">(<span class="number">_</span> sender: UIButton)</span></span> &#123;</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> action1 = <span class="type">UNTextInputNotificationAction</span>(identifier: <span class="string">"replyAction"</span>, title: <span class="string">"回复"</span>, options: [])</span><br><span class="line"><span class="keyword">let</span> action2 = <span class="type">UNNotificationAction</span>(identifier: <span class="string">"enterAction"</span>, title: <span class="string">"进入应用"</span>, options: .foreground)</span><br><span class="line"><span class="keyword">let</span> action3 = <span class="type">UNNotificationAction</span>(identifier: <span class="string">"cancelAction"</span>, title: <span class="string">"忽略"</span>, options: .destructive)</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> category = <span class="type">UNNotificationCategory</span>(identifier: <span class="string">"categoryID"</span>, actions: [action1,action2,action3], intentIdentifiers: [], options: .customDismissAction)</span><br><span class="line"></span><br><span class="line">content.categoryIdentifier = <span class="string">"categoryID"</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> categorySet = <span class="type">Set</span>&lt;<span class="type">UNNotificationCategory</span>&gt;()</span><br><span class="line">categorySet.insert(category)</span><br><span class="line"><span class="type">UNUserNotificationCenter</span>.current().setNotificationCategories(categorySet)</span><br><span class="line"></span><br><span class="line"><span class="type">UNUserNotificationCenter</span>.current().delegate = <span class="keyword">self</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> sounnd = <span class="type">UNNotificationSound</span>.<span class="keyword">default</span>()</span><br><span class="line">content.sound = sounnd</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> trigger = <span class="type">UNTimeIntervalNotificationTrigger</span>(timeInterval: <span class="number">5</span>, repeats: <span class="literal">false</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> requestID = <span class="string">"requestID"</span></span><br><span class="line"><span class="keyword">let</span> request = <span class="type">UNNotificationRequest</span>(identifier: requestID, content: content, trigger: trigger)</span><br><span class="line"></span><br><span class="line"><span class="type">UNUserNotificationCenter</span>.current().add(request) &#123; (error) <span class="keyword">in</span></span><br><span class="line"><span class="keyword">guard</span> <span class="keyword">let</span> error1 = error <span class="keyword">else</span> &#123; <span class="keyword">return</span> &#125;</span><br><span class="line"><span class="built_in">print</span>(error1)</span><br><span class="line">&#125;</span><br><span class="line">&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>代理</strong></p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">extension</span> <span class="title">ViewController</span> : <span class="title">UNUserNotificationCenterDelegate</span></span>&#123;</span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">userNotificationCenter</span><span class="params">(<span class="number">_</span> center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping <span class="params">()</span></span></span> -&gt; <span class="type">Void</span>) &#123;</span><br><span class="line"><span class="keyword">let</span> categoryID = response.notification.request.content.categoryIdentifier</span><br><span class="line"><span class="comment">//        Identifier 来分辨</span></span><br><span class="line"><span class="keyword">if</span> categoryID == <span class="string">"categoryID"</span>&#123;</span><br><span class="line"><span class="keyword">switch</span> response.actionIdentifier &#123;</span><br><span class="line"><span class="keyword">case</span> <span class="string">"replyAction"</span>: <span class="comment">//文字回复</span></span><br><span class="line"><span class="keyword">let</span> textResponse = response <span class="keyword">as</span>! <span class="type">UNTextInputNotificationResponse</span></span><br><span class="line"><span class="keyword">let</span> userText = textResponse.userText</span><br><span class="line"><span class="built_in">print</span>(userText)</span><br><span class="line"><span class="keyword">case</span> <span class="string">"enterAction"</span>:</span><br><span class="line"><span class="built_in">print</span>(<span class="string">"进入应用"</span>)</span><br><span class="line"><span class="keyword">case</span> <span class="string">"cancelAction"</span>:</span><br><span class="line"><span class="built_in">print</span>(<span class="string">"忽略"</span>)</span><br><span class="line"><span class="keyword">default</span>:</span><br><span class="line"><span class="built_in">print</span>(<span class="string">"未能识别\(response.actionIdentifier)"</span>)</span><br><span class="line">&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//调一下</span></span><br><span class="line">completionHandler()</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// MARK: - 只有当前处于前台才会走，加上返回方法，使在前台显示信息</span></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">userNotificationCenter</span><span class="params">(<span class="number">_</span> center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping <span class="params">(UNNotificationPresentationOptions)</span></span></span> -&gt; <span class="type">Void</span>) &#123;</span><br><span class="line">completionHandler([.badge,.alert,.sound])</span><br><span class="line">&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>删除通知</strong></p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">UNUserNotificationCenter</span>.current().removeAllPendingNotificationRequests()</span><br><span class="line"><span class="type">UNUserNotificationCenter</span>.current().removeDeliveredNotifications(withIdentifiers: &lt;#<span class="type">T</span>##[<span class="type">String</span>]#&gt;)</span><br></pre></td></tr></table></figure>
<h4 id="自定义通知样式"><a href="#自定义通知样式" class="headerlink" title="自定义通知样式"></a>自定义通知样式</h4><p><strong>在Xcode中File-&gt;New-&gt;Targe会出现下面的视图</strong></p>
<p><img src="https://ws4.sinaimg.cn/large/006tNbRwly1fwjdz68s9lj314w0ta0xl.jpg" alt=""></p>
<p><img src="https://ws2.sinaimg.cn/large/006tNbRwly1fwje00e5uqj30du04a0tc.jpg" alt=""></p>
<p><strong>在通知的Info.plist文件中可以设置这些值</strong></p>
<blockquote>
<p><strong>UNNotificationExtensionCategory就是UNNotificationCategory的标识,下面的category</strong></p>
</blockquote>
<p><img src="https://ws4.sinaimg.cn/large/006tNbRwly1fwjftrykf8j316w0fywj1.jpg" alt=""></p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">extension</span> <span class="title">ViewController</span> </span>&#123;</span><br><span class="line"><span class="meta">@IBAction</span> <span class="function"><span class="keyword">func</span> <span class="title">actionNotifi</span><span class="params">(<span class="number">_</span> sender: UIButton)</span></span> &#123;</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> action1 = <span class="type">UNTextInputNotificationAction</span>(identifier: <span class="string">"replyAction"</span>, title: <span class="string">"回复"</span>, options: [])</span><br><span class="line"><span class="keyword">let</span> action2 = <span class="type">UNNotificationAction</span>(identifier: <span class="string">"enterAction"</span>, title: <span class="string">"进入应用"</span>, options: .foreground)</span><br><span class="line"><span class="keyword">let</span> action3 = <span class="type">UNNotificationAction</span>(identifier: <span class="string">"cancelAction"</span>, title: <span class="string">"忽略"</span>, options: .destructive)</span><br><span class="line"></span><br><span class="line">	<span class="keyword">let</span> category = <span class="type">UNNotificationCategory</span>(identifier: <span class="string">"categoryID"</span>, actions: 			[action1,action2,action3], intentIdentifiers: [], options: .customDismissAction)</span><br><span class="line">	content.categoryIdentifier = <span class="string">"categoryID"</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> categorySet = <span class="type">Set</span>&lt;<span class="type">UNNotificationCategory</span>&gt;()</span><br><span class="line">categorySet.insert(category)</span><br><span class="line"><span class="type">UNUserNotificationCenter</span>.current().setNotificationCategories(categorySet)</span><br><span class="line"><span class="type">UNUserNotificationCenter</span>.current().delegate = <span class="keyword">self</span></span><br><span class="line"><span class="keyword">let</span> sounnd = <span class="type">UNNotificationSound</span>.<span class="keyword">default</span></span><br><span class="line">content.sound = sounnd</span><br><span class="line"><span class="keyword">let</span> trigger = <span class="type">UNTimeIntervalNotificationTrigger</span>(timeInterval: <span class="number">5</span>, repeats: <span class="literal">false</span>)</span><br><span class="line"><span class="keyword">let</span> requestID = <span class="string">"requestID"</span></span><br><span class="line"><span class="keyword">let</span> request = <span class="type">UNNotificationRequest</span>(identifier: requestID, content: content, trigger: trigger)</span><br><span class="line"></span><br><span class="line"><span class="type">UNUserNotificationCenter</span>.current().add(request) &#123; (error) <span class="keyword">in</span></span><br><span class="line"><span class="keyword">guard</span> <span class="keyword">let</span> error1 = error <span class="keyword">else</span> &#123; <span class="keyword">return</span> &#125;</span><br><span class="line"><span class="built_in">print</span>(error1)</span><br><span class="line">&#125;</span><br><span class="line">&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>UNNotificationExtensionCategory也可以为数组,意味着多个通知共用一套UI</strong></p>
<p><img src="https://ws4.sinaimg.cn/large/006tNbRwly1fwjfuwnp7tj31jw0iw43t.jpg" alt=""></p>
<blockquote>
<p><strong>UNNotificationExtensionDefaultContentHidden是否隐藏默认内容，如果设为YES，默认内容会被隐藏。</strong></p>
</blockquote>
<blockquote>
<p><strong>UNNotificationExtensionInitialContentSizeRatio 宽高比例浮点数;比如 0.5高度是宽度的一般 ,代码未启动之前启动通知是需要用这个从info.plist文件UNNotificationExtensionInitialContentSizeRatio设置通知的大小, viewDidLoad 中使用 self.preferredContentSize 直接设置 Size</strong></p>
</blockquote>
<p><strong>远程通知相关</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">    &quot;aps&quot;:&#123;</span><br><span class="line">        &quot;alert&quot;:&quot;Testing.. (34)&quot;,</span><br><span class="line">        &quot;badge&quot;:1,</span><br><span class="line">        &quot;sound&quot;:&quot;default&quot;,</span><br><span class="line">        &quot;category&quot;:&quot;categoryID1&quot;</span><br><span class="line">     &#125;</span><br><span class="line">&#125;</span><br><span class="line">可以使用 category 的Value 选取UI</span><br></pre></td></tr></table></figure>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2016/10/12/获取高度/" rel="next" title="获取高度">
                <i class="fa fa-chevron-left"></i> 获取高度
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2016/10/24/终端常见的命令/" rel="prev" title="终端常见的命令">
                终端常见的命令 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
    </div>
  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            Table of Contents
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            Overview
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/images/10.png"
                alt="Erhqe" />
            
              <p class="site-author-name" itemprop="name">Erhqe</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">70</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                
                  <span class="site-state-item-count">31</span>
                  <span class="site-state-item-name">categories</span>
                
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                
                  <span class="site-state-item-count">1</span>
                  <span class="site-state-item-name">tags</span>
                
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#本地通知"><span class="nav-number">1.</span> <span class="nav-text">本地通知</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#UNMutableNotificationContent-内容"><span class="nav-number">1.1.</span> <span class="nav-text">UNMutableNotificationContent-内容</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#UNNotificationSound-声音"><span class="nav-number">1.2.</span> <span class="nav-text">UNNotificationSound - 声音</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#UNPushNotificaitonTrigger-触发器"><span class="nav-number">1.3.</span> <span class="nav-text">UNPushNotificaitonTrigger - 触发器</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#UNTimeIntervalNotificaitonTrigger-时间触发器"><span class="nav-number">1.4.</span> <span class="nav-text">UNTimeIntervalNotificaitonTrigger - 时间触发器</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#UNCalendarNotificaitonTrigger-日期触发器"><span class="nav-number">1.5.</span> <span class="nav-text">UNCalendarNotificaitonTrigger - 日期触发器</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#UNLocationNotificaitonTrigger-位置触发器"><span class="nav-number">1.6.</span> <span class="nav-text">UNLocationNotificaitonTrigger - 位置触发器</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#UNNotificationRequest-通知请求"><span class="nav-number">1.7.</span> <span class="nav-text">UNNotificationRequest - 通知请求</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#UNNotificationationAttachment-附件"><span class="nav-number">1.8.</span> <span class="nav-text">UNNotificationationAttachment - 附件</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#自定义通知样式"><span class="nav-number">1.9.</span> <span class="nav-text">自定义通知样式</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Erhqe</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Muse</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  










  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src="//unpkg.com/valine/dist/Valine.min.js"></script>
  
  <script type="text/javascript">
    var GUEST = ['nick','mail','link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item=>{
      return GUEST.indexOf(item)>-1;
    });
    new Valine({
        el: '#comments' ,
        verify: false,
        notify: false,
        appId: 'TiAY2J24ECwQH9uLYsoJpgIK-gzGzoHsz',
        appKey: 'Y9PiVrPXVmfNsv4Aa6cPsADG',
        placeholder: 'Just go go',
        avatar:'mm',
        guest_info:guest,
        pageSize:'10' || 10,
    });
  </script>



  





  

  

  

  
  

  

  

  

</body>
</html>
